// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.layers;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

import org.dengues.reports.editor.design.figures.ImageConstants;
import org.dengues.reports.editor.design.parts.TableEditPart;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.editparts.GridLayer;

/**
 * Paint the grid
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-22 qiang.zhang $
 * 
 */
public class TableGridLayer extends GridLayer {

    private final TableEditPart source;

    /**
     * Constructor
     * 
     * @param rows
     * @param cells
     */
    public TableGridLayer(TableEditPart source) {
        super();
        this.source = source;
    }

    /**
     * Create the list of all the images to be displayed.
     * 
     * @param x the x-cordinator of the base image.
     * @param y the y-cordinator of the base image.
     * @param size
     * @param repeat
     * @param rectangle
     * @return the list of all the images to be displayed.
     */
    private ArrayList createImageList(int x, int y, Dimension size, int repeat, Rectangle rectangle) {
        Rectangle area = rectangle;

        ArrayList yList = new ArrayList();

        if ((repeat & ImageConstants.REPEAT_Y) == 0) {
            yList.add(new Point(x, y));
        } else {
            int i = 0;
            while (y + size.height * i + size.height > area.y) {
                yList.add(new Point(x, y + size.height * i));
                i--;
            }

            i = 1;
            while (y + size.height * i < area.y + area.height) {
                yList.add(new Point(x, y + size.height * i));
                i++;
            }
        }
        ArrayList xyList = new ArrayList();

        Iterator iter = yList.iterator();
        while (iter.hasNext()) {
            Point point = (Point) iter.next();

            if ((repeat & ImageConstants.REPEAT_X) == 0) {
                xyList.add(point);
            } else {
                int i = 0;
                while (point.x + size.width * i + size.width > area.x) {
                    xyList.add(new Point(point.x + size.width * i, point.y));
                    i--;
                }

                i = 1;
                while (point.x + size.width * i < area.x + area.width) {
                    xyList.add(new Point(point.x + size.width * i, point.y));
                    i++;
                }
            }
        }
        yList.clear();

        return xyList;
    }

    /**
     * Sorter to be used to sort the rows with row number
     * 
     */
    public static class NumberComparator implements Comparator {

        /*
         * (non-Javadoc)
         * 
         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
         */
        public int compare(Object o1, Object o2) {
            return 0;
        }
    }
}